/*
 * Copyright (c) Huawei Device Co., Ltd. 2023. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "reader_stub.h"
#include "common_utils.h"
#include "isecure_element_session.h"
#include "loghelper.h"
#include "message_parcel.h"
#include "se_common_exception.h"
#include "se_service_ipc_interface_code.h"

using namespace std;

namespace OHOS {
namespace SE {
namespace OMAPI {

int ReaderStub::OnRemoteRequest(uint32_t code,         /* [in] */
                                MessageParcel& data,   /* [in] */
                                MessageParcel& reply,  /* [out] */
                                MessageOption& option) /* [in] */
{
    InfoLog("ReaderStub::OnRemoteRequest occur, code is %{public}d", code);
    if (data.ReadInterfaceToken() != GetDescriptor()) {
        ErrorLog("ReaderStub OnRemoteRequest GetDescriptor failed");
        return ErrorCode::SE_IPC_TOKEN_ERROR;
    }
    switch (code) {
        case static_cast<uint32_t>(SeServiceIpcInterfaceCode::COMMAND_READER_OPEN_SESSION):
            return HandleOpenSession(data, reply);
        case static_cast<uint32_t>(SeServiceIpcInterfaceCode::COMMAND_READER_CLOSE_SESSIONS):
            return HandleCloseSessions(data, reply);
        case static_cast<uint32_t>(SeServiceIpcInterfaceCode::COMMAND_READER_RESET):
            return HandleReset(data, reply);
        case static_cast<uint32_t>(SeServiceIpcInterfaceCode::COMMAND_READER_IS_SE_PRESENT):
            return HandleIsSecureElementPresent(data, reply);
        default:
            return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
    }
}
int ReaderStub::HandleOpenSession(OHOS::MessageParcel& data, OHOS::MessageParcel& reply)
{
    // Note: don't check the IPC permssion, secure element security is by Access Rule.
    sptr<ISecureElementSession> session;
    int statusCode = OpenSession(session);
    if (session) {
        InfoLog("HandleOpenSession add session");
        reply.WriteRemoteObject(session->AsObject());
    }
    reply.WriteInt32(statusCode);
    return ERR_NONE;
}

int ReaderStub::HandleCloseSessions(OHOS::MessageParcel& data, OHOS::MessageParcel& reply)
{
    // Note: don't check the IPC permssion, secure element security is by Access Rule.
    int statusCode = CloseSessions();
    InfoLog("HandleCloseSessions CloseSessions done");
    reply.WriteInt32(statusCode);
    return ERR_NONE;
}

int ReaderStub::HandleReset(OHOS::MessageParcel& data, OHOS::MessageParcel& reply)
{
    // Note: don't check the IPC permssion, secure element security is by Access Rule.
    bool resetStatus = false;
    int statusCode = Reset(resetStatus);
    reply.WriteBool(resetStatus);
    reply.WriteInt32(statusCode);
    return ERR_NONE;
}
int ReaderStub::HandleIsSecureElementPresent(OHOS::MessageParcel& data, OHOS::MessageParcel& reply)
{
    // Note: don't check the IPC permssion, secure element security is by Access Rule.
    bool isPresent = false;
    int statusCode = IsSecureElementPresent(isPresent);
    reply.WriteBool(isPresent);
    reply.WriteInt32(statusCode);
    return ERR_NONE;
}
}  // namespace OMAPI
}  // namespace SE
}  // namespace OHOS